This is a small collection of mostly game example source codes. These source codes are made available to help PlayBasic programmers kick start their game programming journey.
Looking for more source code / tutorials & media, then remember to visit the PlayBasic Resource board on our forums.
This example is basically an asteroids frame work. While it doesn't play a full game of asteroids, it's set up to demonstrate a method of partitioning the games world space to help eliminate unnecessary collision checks. In this case shape intersections.
This demo is based upon the original 2D platformer example from the PlayBasic example pack, the only difference is that this version projects the 2D scene into 3D space. This lets the player view the 2D game in a 3D space.
This code creates a game level with a series of randomly colored boxes on the screen, which are drawn as an image. The image is then imported into a map called "MyMap." The code then reads in a level from data statements, makes the level transparent, and draws it to the screen. The code also creates a collision world from the map, and sets up a sprite and camera.
The sprite can be moved around the screen using arrow keys, and will automatically slide off walls it hits in the collision world. The camera is positioned over the sprite, and will draw what is in view. The game will continue to loop until the space key is pressed.
PlayBasic Code:
MyMap =NewMap(1);
TileWidth=48
TileHeight=48
Number_of_tiles =5; Loop through and draw a series of randomly coloured Boxes to the screenFor lp=1To Number_of_tiles
xpos=lp*tileWidth
BoxC Xpos,0,Xpos+TileWidth,TileHeight,1,RndRGB()Next; Grab the box graphics that we've just drawn as image.
TempImage=GetFreeImage()GetImage TempImage,0,0,xpos+TIleWidth,Tileheight
; Import this image into our previously defined Map. MakeMapGFX MyMAP,TempImage,TileWidth,TileHeight,Number_Of_Tiles,RGB(0,0,0); =======================================================; Read the Level in from the data statements bellow; =======================================================
Read_Level(1,1)Leveltransparent1,1,0; Draw the Level to the screen at Xpos 0 and Ypos 0
Xpos=0
Ypos=0; Create the Collision world
MyCollisionWorld=NewWorld()CaptureToWorld MyCollisionWorld
MakeCollisionWorldFromMap(MyMap,1,xpos,ypos)PartitionWorld MyCollisionWorld,64; restore drawing back to immediate modedrawgfximmediate
PLayerSize =32; Create an image of a circle (for something to look at)Cls0
Size=PLayerSize/2Circlec Size,Size,Size,1,$00ff00GetImage10,0,0,PLayerSize,PLayerSize
preparefximage10; create a sprite CreateSprite1autocenterspritehandle1,trueSpriteImage1,10spriteCollision1,true; Set collision mode to WorldSpriteCollisionMode1,4SpriteCollisionWorld1,MyCollisionWorld
; Set the Sprites Collision Circle to just smaller than then players; image. In this example the players image is also a circle.SpriteCollisionRadius1,(PlayerSize/2)-2; SpriteCollisionDebug 1,truePositionSprite1,150,150; Create a CameraCreateCamera1DoCaptureToSceneClsSceneCapturedepth100DrawMap MyMap,1,0,0; MOve the Sprite around (8 way movement)
Speed=5
Xspeed=0
Yspeed=0ifLeftKey()then Xspeed=-Speed
ifRightKey()then Xspeed=Speed
ifUpKey()then Yspeed=-Speed
ifDownKey()then Yspeed=Speed
if Xspeed<>0or Yspeed<>0; MOve the sprite, this will auto slide the sprite; off any wall it hits in the Collision World MoveSprite1,xspeed,yspeed
endifDrawAllSprites;POsition the camera over the sprite
Xpos=GetSpriteX(1)-(GetScreenWidth()/2)
Ypos=GetSpriteY(1)-(GetScreenHeight()/2)POsitionCamera1,Xpos,Ypos
; Tell the camera to draw whats in viweDrawcamera1Syncloopspacekey()end; =============================================================================; This Function converts a map Level into collision world, which is nothing; more than drawing lines around the bounding area around of the tiles. It; considers tile zero to be transparent (which is soft). Everything else is; think is hard ;; once the map is a collision world you can run ray interest on it, sliding; collision among other things..; =============================================================================AcresetConstant EdgeState_Searching =ac(1)Constant EdgeState_FoundStart =ac(1)Function MakeCollisionWorldFromMap(thisMap,Thislevel,xpos,ypos)
LevelWidth =GetLevelWidth(Thismap,thislevel)
LevelHeight =GetLevelHeight(Thismap,thislevel)
BlockWidth=GetMapBLockWidth(ThisMap)
BlockHeight=GetMapBLockHeight(ThisMap); ====================================================================; #1 first Pass over the level looking for blocks running Left to Right; =====================================================================Dot0,0
Y=Ypos
For Ylp=0to LevelHeight
Y2=Y+BlockHeight
X=Xpos
TopEdgeState=EdgeSTate_Searching
TopStartXpos=Xpos
BotEdgeState=EdgeSTate_Searching
BotStartXpos=Xpos
For Xlp=0to LevelWidth
CurrentTile =PeekLevelTile(ThisMap,Thislevel,Xlp,Ylp); Check if this tile is not our transparent tile zero If CurrentTile<>0; Check the Y position to make sure we don't step off the Level arrayiF Ylp>0; Grab the tile above our current tile
AboveCurrentTile =PeekLevelTile(Thismap,Thislevel,Xlp,Ylp-1); Check if this tile is transparentif AboveCurrentTile=0; Since there's no tile above then this must be a top; edge of the blockif TopEdgeState=EdgeSTate_Searching
inc TopEdgeState
TopStartXpos=X
endifelseif TopEdgeState=EdgeSTate_FoundStart
TopEdgeState=EdgeSTate_Searching
;' Line TopStartXpos,y,X,yLine x,y,TopStartXpos,y
endifendifEndif; Check the Y position to make sure we don't step off the Level arrayif Ylp<LevelHeight
; Grab the tile above our current tile
BellowCurrentTile =PeekLevelTile(Thismap,Thislevel,Xlp,Ylp+1); Check if this tile is transparentif BellowCurrentTile=0; Since there's no tile above then this must be a top; edge of the blockif BotEdgeState=EdgeSTate_Searching
inc BotEdgeState
BotStartXpos=X
endifelseif BotEdgeState=EdgeSTate_FoundStart
BotEdgeState=EdgeSTate_Searching
Line BotStartXpos,y2,X,y2
endifendifendifelse; close edgesif TopEdgeState=EdgeSTate_FoundStart
TopEdgeState=EdgeSTate_Searching
Line x,y,TopStartXpos,y
endifif BotEdgeState=EdgeSTate_FoundStart
BotEdgeState=EdgeSTate_Searching
Line BotStartXpos,y2,X,y2
endifendif
X=X+BlockWidth
next
Y=Y+BlockHeight
next; ====================================================================; #2 now we repeat this process, but scanning from Top to Bottom; =====================================================================
X=Xpos
For Xlp=0to LevelWidth
X2=X+BlockWidth
Y=Ypos
LeftEdgeState=EdgeSTate_Searching
LeftStartYpos=Y
RightEdgeState=EdgeSTate_Searching
RightStartYpos=Y
For ylp=0to LevelHeight
CurrentTile =PeekLevelTile(ThisMap,Thislevel,Xlp,Ylp); Check if this tile is not our transparent tile zero If CurrentTile<>0; Check the Y position to make sure we don't step off the Level arrayiF Xlp>0; Grab the tile to the LEFT of our current tile
PreviousTile =PeekLevelTile(Thismap,Thislevel,Xlp-1,Ylp); Check if this tile is transparentif PreviousTile=0; Since there's no tile then this must be the LEFT; edge of the blockif LeftEdgeState=EdgeSTate_Searching
inc LeftEdgeState
LeftStartYpos=y
endifelseif LeftEdgeState=EdgeSTate_FoundStart
LeftEdgeState=EdgeSTate_Searching
Line x,LeftStartYpos,x,y
endifendifEndif; Check the X position to make sure we don't step off the Level arrayif Xlp<LevelWidth
; Grab the tile above our current tile
NextTile =PeekLevelTile(Thismap,Thislevel,Xlp+1,Ylp); Check if this tile is transparentif NextTile=0; Since there's no tile on the RIGHT then this must be a right; edge of the blockif RightEdgeState=EdgeSTate_Searching
inc RightEdgeState
RightStartYpos=Y
endifelseif RightEdgeState=EdgeSTate_FoundStart
RightEdgeState=EdgeSTate_Searching
; Line x2,RightStartYpos,x2,yLine x2,y,x2,RightStartYpos
endifendifendifelse; close edges (if open)if LeftEdgeState=EdgeSTate_FoundStart
LeftEdgeState=EdgeSTate_Searching
Line x,LeftStartYpos,x,y
endifif RightEdgeState=EdgeSTate_FoundStart
RightEdgeState=EdgeSTate_Searching
;' Line x2,RightStartYpos,x2,yLine x2,y,x2,RightStartYpos
endifendif
Y=Y+BlockHeight
next
X=X+BlockWidth
nextEndFunction` *=----------------------------------------------------------------=*` -------- Read Map ---------` *=----------------------------------------------------------------=*Function Read_Level(ThisMap,ThisLevel)
Map_Width=readdata()
Map_Height=readdata(); Init the Level array within this Mapcreatelevel ThisMap,ThisLevel,Map_Width+1,Map_Height+1for zlp=0to map_Height-1for xlp=0to map_width-1pokeleveltile ThisMap,Thislevel,xlp,zlp,ReadData()next xlp
next zlp
EndFunction Map_Width,Map_Depth
data32data42data1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1data1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1data1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1data1,0,0,0,0,2,2,2,2,0,2,0,0,0,1,0,1,0,1,0,1,0,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1,1,0,0,0,0,1,0,0,1data1,0,1,0,0,1,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1data1,0,1,0,0,1,0,1,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,1data1,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1data1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1data1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1data1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
This is a fairly simple example of how you could set up a street fighter/mortal combat styled fighting game. The demo doesn't include any combat, just the player movement and a perspective camera so the scene has depth.
This is a loose remake of Xenon 2000 scrolling environments, which is a game the bitmap brothers made for magazine series. While the artwork is mostly from that example, all of the code is PlayBasic. The focus of the demo wasn't to make a vertical shoot'em up, but to demonstrate how multi player co-op could achieved.
Release Type:
The source code & tutorials found on this site are released as license ware for PlayBasic Users. No Person or Company may redistribute any file (tutorial / source code or media files) from this site, without explicit written permission.